<template>
  <div class="app-container">
    <!-- 添加或修改电子签章申请对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" style="margin-left: 50px;margin-top: 30px;width: 700px">
        <el-form-item label="员工编号" prop="eno">
          <el-input v-model="form.eno" placeholder="请输入员工编号" />
        </el-form-item>
        <el-form-item label="电子签章" prop="stampName">
          <el-input v-model="form.stampName" placeholder="请输入电子签章" />
        </el-form-item>
        <el-form-item label="用途描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">申请</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
import { listStamp, getStamp, delStamp, addStamp, updateStamp } from "@/api/system/stamp";
import {addEvection} from "@/api/system/evection";

export default {
  name: "Stamp",
  dicts: ['stamp'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电子签章申请表格数据
      stampList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eno: null,
        stampName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        eno: [
          { required: true, message: "员工编号不能为空", trigger: "blur" }
        ],
        stampName: [
          { required: true, message: "电子签章不能为空", trigger: "blur" }
        ],
        createdAt: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updatedAt: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onSubmit() {
      addEvection(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.reset();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        eno: null,
        stampName: null,
        description: null,
        status: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电子签章申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStamp(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电子签章申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStamp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStamp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除电子签章申请编号为"' + ids + '"的数据项？').then(function() {
        return delStamp(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/stamp/export', {
        ...this.queryParams
      }, `stamp_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
